﻿using System.Collections.Generic;
using System.Linq;
using NHDay_Linq.Domain;
using NHibernate;
using NUnit.Framework;
using NHibernate.Linq;
using NHDay_Linq.ProviderExtensions;

namespace NHDay_Linq
{
	[TestFixture]
	public class T07_ExtendingLinqTests : TestBase
	{
		[TestFixtureSetUp]
		public void FixtureSetup()
		{
			CreateDatabase();
		}

		[Test]
		public void T01_IsLike_Success()
		{
			using (ISession session = SessionFactory.OpenSession())
			using (ITransaction tx = session.BeginTransaction())
			{
				var magnus = session.Query<Adult>().Where(a => a.FirstName.IsLike("%gnu%")).Single();

				tx.Commit();

				Assert.IsNotNull(magnus);
				Assert.AreEqual("Magnus", magnus.FirstName);
			}
		}

		[Test]
		public void T02_CustomGetMonth_Success()
		{
			using (ISession session = SessionFactory.OpenSession())
			using (ITransaction tx = session.BeginTransaction())
			{
				var magnus = session.Query<Adult>().Where(a => a.BirthDate.CustomGetMonth() == 1).Single();

				tx.Commit();

				Assert.IsNotNull(magnus);
				Assert.AreEqual("Magnus", magnus.FirstName);
			}
		}

		[Test]
		public void T03_CustomGetMonth_Success_Projection()
		{
			using (ISession session = SessionFactory.OpenSession())
			using (ITransaction tx = session.BeginTransaction())
			{
				var list = session.Query<Adult>().Select(a => a.BirthDate.CustomGetMonth()).ToList();

				tx.Commit();

				Assert.IsNotNull(list);

			}
		}
	}
}
